﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Gqq.Csharpindepth.Common
{
    /// <summary>
    /// 添加附加方法
    /// </summary>
    public static class ExtensionClass
    {
        /// <summary>
        /// ArrayList的扩展Sort方法
        /// </summary>
        /// <param name="lsts"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static ArrayList Sort(this ArrayList lsts,IListCompare action)
        {
//            action.Compare()
            //最简单的“冒泡排序法”
            bool cFlg = false;
            for (int i = 0; i < lsts.Count - 1;i++ )
            {
                for (int j = lsts.Count - 2; j >= i; j--)
                {
                    //如果元素比它后面的元素大，就交换
                    if (action.Compare(lsts[j], lsts[j + 1]) > 0)
                    {
                        var temp = lsts[j];
                        lsts[j] = lsts[j + 1];
                        lsts[j + 1] = temp;
                        cFlg = true;
                    }
                }
                if (!cFlg)
                    break;
            }
            return lsts;
        }

        /// <summary>
        /// 泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lsts"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static List<T> Sort<T>(this List<T> lsts, IListCompare<T> action)
        {
            //            action.Compare()
            //最简单的“冒泡排序法”
            bool cFlg = false;
            for (int i = 0; i < lsts.Count - 1; i++)
            {
                for (int j = lsts.Count - 2; j >= i; j--)
                {
                    //如果元素比它后面的元素大，就交换
                    if (action.Compare(lsts[j], lsts[j + 1]) > 0)
                    {
                        var temp = lsts[j];
                        lsts[j] = lsts[j + 1];
                        lsts[j + 1] = temp;
                        cFlg = true;
                    }
                }
                if (!cFlg)
                    break;
            }
            return lsts;
        }

        /// <summary>
        /// 写一个返回比较字符串字符串的扩展方法
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="sources"> </param>
        /// <param name="selector"></param>
        /// <returns></returns>
        public static string Max<TSource>(this IEnumerable<TSource> sources, Func<TSource, string> selector)
        {
            string max = null;
            foreach (var source in sources)
            {
                var value = selector(source);
                if (max == null || String.CompareOrdinal(value, max) > 0)
                    max = value;
            }
            return max;
        }

        public static IEnumerable<TSource> WhereGqq<TSource>(this IEnumerable<TSource> sources, GqqFunc<TSource, bool> selector)
        {
//            var newSources = new IEnumerable<TSource>();
            List<TSource> lsts = sources.Where(s => selector(s)).ToList();
//            foreach (var source in sources)
//            {
//                if (selector(source))
//                    lsts.Add(source);
//            }
            return lsts.AsEnumerable();
        }

        public static IEnumerable<TSource> WhereGqq<TSource>(this IEnumerable<TSource> sources, GqqFunc<TSource, int> selector, int value = 0)
        {
            //            var newSources = new IEnumerable<TSource>();
            List<TSource> lsts = new List<TSource>();
            foreach (var source in sources)
            {
                if (selector(source) >= value)
                    lsts.Add(source);
            }
            return lsts.AsEnumerable();
        }

    }
}
